<?php

namespace App\Jobs;

use App\Lead;
use App\Notifications\JpushMsg;
use App\Notifications\JpushNotification;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class Send implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $jpushMsg;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Lead $lead)
    {
        $this->jpushMsg=$lead;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
       // User::whereHas('');
        $lead=$this->jpushMsg;
        $user=  User::whereHas('project',function($q)use($lead){$q->where('5kcrm_project.project_id',$lead->project_id);})->inRandomOrder()->get();
        foreach ($user as $item){
            try {
               $item->notify(new \App\Notifications\LeadsNotification($this->jpushMsg));
                \Log::info('推送成功',$item->toArray());
            }catch (\Exception $exception){
                \Log::error('推送错误',[$exception->getMessage()]);
            }
        }
    }
}
